﻿using DingVision.Infrastructure.Events;
using DingVision.Infrastructure.Helpers;
using DingVision.Infrastructure.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DingVision.Axis.ViewModels
{
    public class PointPageViewModel : BindableBase
    {
        #region 变量
        private readonly IEventAggregator eventAggregator;
        #endregion
        #region 属性绑定
        private ObservableCollection<RobotPoint> robotPoints = new ObservableCollection<RobotPoint>();
        

        public ObservableCollection<RobotPoint> RobotPoints
        {
            get { return robotPoints; }
            set { SetProperty(ref robotPoints, value); }
        }
        private bool buttonIsEnabled = false;
        public bool ButtonIsEnabled
        {
            get { return buttonIsEnabled; }
            set { SetProperty(ref buttonIsEnabled, value); }
        }
        private int robotPointsSelectedIndex = -1;
        public int RobotPointsSelectedIndex
        {
            get { return robotPointsSelectedIndex; }
            set { SetProperty(ref robotPointsSelectedIndex, value); }
        }
        private bool deletButtonIsEnabled = false;
        public bool DeletButtonIsEnabled
        {
            get { return deletButtonIsEnabled; }
            set { SetProperty(ref deletButtonIsEnabled, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand cellEditEndingEventCommand;
        public DelegateCommand CellEditEndingEventCommand =>
            cellEditEndingEventCommand ?? (cellEditEndingEventCommand = new DelegateCommand(ExecuteCellEditEndingEventCommand));
        private DelegateCommand _deletPointCommand;
        public DelegateCommand DeletPointCommand =>
            _deletPointCommand ?? (_deletPointCommand = new DelegateCommand(ExecuteDeletPointCommand));
        private DelegateCommand savePointCommand;
        public DelegateCommand SavePointCommand =>
            savePointCommand ?? (savePointCommand = new DelegateCommand(ExecuteSavePointCommand));
        private DelegateCommand restorePointCommand;
        public DelegateCommand RestorePointCommand =>
            restorePointCommand ?? (restorePointCommand = new DelegateCommand(ExecuteRestorePointCommand));
        private DelegateCommand selectionChangedEventCommand;
        public DelegateCommand SelectionChangedEventCommand =>
            selectionChangedEventCommand ?? (selectionChangedEventCommand = new DelegateCommand(ExecuteSelectionChangedEventCommand));



   
        void ExecuteCellEditEndingEventCommand()
        {
            ButtonIsEnabled = true;
            RobotPoints[RobotPointsSelectedIndex].Isrecord = true;
            DeletButtonIsEnabled = true;
        }
        void ExecuteDeletPointCommand()
        {
            if (MessageBox.Show($"删除点# '{RobotPoints[RobotPointsSelectedIndex].PIndex} - {RobotPoints[RobotPointsSelectedIndex].PName}' 吗？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                RobotPoints[RobotPointsSelectedIndex].PName = "未定义";
                RobotPoints[RobotPointsSelectedIndex].X = 0;
                RobotPoints[RobotPointsSelectedIndex].Y = 0;
                RobotPoints[RobotPointsSelectedIndex].Z = 0;
                RobotPoints[RobotPointsSelectedIndex].R = 0;
                RobotPoints[RobotPointsSelectedIndex].Isrecord = false;
                RobotPointsSelectedIndex++;
                ButtonIsEnabled = true;
            }
        }
        void ExecuteSavePointCommand()
        {
            JsonHelp.JsonWrite(RobotPoints, System.IO.Path.Combine(System.Environment.CurrentDirectory, "RobotPoints.json"));
            eventAggregator.GetEvent<AxisPointUpdateAllPointsEvent>().Publish();
            ButtonIsEnabled = false;
        }
        void ExecuteRestorePointCommand()
        {
            if (MessageBox.Show("从磁盘恢复所有点坐标吗？", "确认", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                RobotPoints = JsonHelp.JsonRead<ObservableCollection<RobotPoint>>(System.IO.Path.Combine(System.Environment.CurrentDirectory, "RobotPoints.json"));
                eventAggregator.GetEvent<AxisPointUpdateAllPointsEvent>().Publish();
                ButtonIsEnabled = false;
            }
        }
        void ExecuteSelectionChangedEventCommand()
        {
            if (RobotPointsSelectedIndex >= 0)
            {
                if (RobotPoints[RobotPointsSelectedIndex].Isrecord)
                {
                    DeletButtonIsEnabled = true;
                }
                else
                {
                    DeletButtonIsEnabled = false;
                }
            }
         
        }
        #endregion
        #region 构造函数
        public PointPageViewModel(IEventAggregator _eventAggregator)
        {            
            eventAggregator = _eventAggregator;
            eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
            eventAggregator.GetEvent<AxisJogToPointEvent>().Subscribe(AxisJogToPointEventReceived);
        }

        private void AxisJogToPointEventReceived(RobotPointItem obj)
        {
           RobotPoint rp =  RobotPoints.FirstOrDefault(a => a.PIndex == obj.PIndex);
            rp.PName = obj.PName;
            rp.X = obj.X;
            rp.Y = obj.Y;
            rp.Z = obj.Z;
            rp.R = obj.R;
            rp.Isrecord = true;
            ButtonIsEnabled = true;
        }
        #endregion
        #region 自定义函数
        private void ShellSendEventReceived(string msg)
        {
            switch (msg)
            {
                case "Loaded":
                    try
                    {
                        RobotPoints = JsonHelp.JsonRead<ObservableCollection<RobotPoint>>(System.IO.Path.Combine(System.Environment.CurrentDirectory, "RobotPoints.json"));                      
                    }
                    catch { }
                    break;
                default:
                    break;
            }
        }
        #endregion
    }
}
